package com.yx.cloud.network.ap;

import com.yx.cloud.network.DataPacket;
import com.yx.cloud.network.ble.Cmd;

import java.util.ArrayList;
import java.util.List;

public class ApDataHandler {
    public static List<ApDataPacket> packageData(Cmd command) {
        byte cmd = command.getCmd();
        byte[] content = command.getContent();
        if (content == null) {
            //只有命令的空数据包
            List<ApDataPacket> dataPacketList = new ArrayList<>();
            dataPacketList.add(sliceCmd((byte) 1, (byte) 1, cmd, null));
            return dataPacketList;
        }

        int remain = content.length;
        int position = 0;
        int currentPackSize;
        byte order = 0;
        byte packageSum = (byte) Math.ceil(content.length * 1f / ApDataPacket.REAL_SIZE);

        List<ApDataPacket> dataPacketList = new ArrayList<>();
        while (remain > 0) {
            order++;
            if (remain >= ApDataPacket.REAL_SIZE) {
                currentPackSize = ApDataPacket.REAL_SIZE;
                byte[] pack = new byte[currentPackSize];
                System.arraycopy(content, position, pack, 0, pack.length);
                dataPacketList.add(sliceCmd(order, packageSum, cmd, pack));
            } else {
                currentPackSize = remain;
                byte[] pack = new byte[currentPackSize];
                System.arraycopy(content, position, pack, 0, pack.length);
                dataPacketList.add(sliceCmd(order, packageSum, cmd, pack));
            }

            position += currentPackSize;
            remain -= currentPackSize;

        }
        return dataPacketList;
    }

    private static ApDataPacket sliceCmd(byte order, byte packageSum, byte cmd, byte[] raw) {
        byte[] packData = null;
        if (raw != null) {
            packData = new byte[7 + raw.length];
            packData[0] = ApDataPacket.HEADER;
            packData[1] = order;
            packData[2] = packageSum;
            byte[] lengthByte = ApDataPacket.convertShortToTwoBytes((short) (raw.length + 1));
            packData[3] = lengthByte[0];
            packData[4] = lengthByte[1];
            packData[5] = cmd;
            System.arraycopy(raw, 0, packData, 6, raw.length);
            packData[5 + raw.length + 1] = getCheckSum(cmd, raw);
        } else {
            packData = new byte[7];
            packData[0] = ApDataPacket.HEADER;
            packData[1] = order;
            packData[2] = packageSum;
            byte[] lengthByte = ApDataPacket.convertShortToTwoBytes((short) 1);
            packData[3] = lengthByte[0];
            packData[3] = lengthByte[1];
            packData[5] = cmd;
            packData[6] = getCheckSum(cmd, null);
        }


        return new ApDataPacket(packData);
    }

    private static byte getCheckSum(byte cmd, byte[] contentSlice) {
        byte sum = cmd;

        if (contentSlice != null) {
            for (int i = 0; i < contentSlice.length; i++) {
                sum += contentSlice[i];
            }
        }

        return sum;
    }


    public static boolean unPackageData(Cmd cmd, byte[] packData) {
        boolean isValid = ApDataPacket.validateSlicePackage(packData);

        if (!isValid) {
            return false;
        }

        ApDataPacket dataPacket = new ApDataPacket(packData);
        cmd.addSlicePackage(dataPacket);

        //如果两个命令不同 丢弃前一个指令 重新接收新的指令
        if (cmd.getCmd() != packData[ApDataPacket.CMD_START]) {
            cmd.clear();
        }

        cmd.addSlicePackage(dataPacket);

        cmd.checkComplete();

        return cmd.isComplete();
    }


}
